package ossFileinfo

import (
	"common/comCopier"
	"common/dao/model"
	"common/utils"
	"context"
	"gorm.io/gorm"

	"file/internal/svc"
	"file/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetGroupNameLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetGroupNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGroupNameLogic {
	return &GetGroupNameLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetGroupNameLogic) GetGroupName() (*types.Names, error) {
	// todo: add your logic here and delete this line
	var (
		names = make([]string, 0)
		resp  = types.Names{}
	)
	db := l.svcCtx.DB
	table := db.Table(model.TableNameSysFile)
	err := table.Transaction(func(tx *gorm.DB) error {

		if err2 := tx.Distinct("group_name").Pluck("group_name", &names).Error; err2 != nil {
			return err2
		}
		return nil
	})
	if err != nil {
		return nil, err
	}
	names = utils.FilterEmptyStrings(names)
	err = comCopier.CopyWithOption(&resp.Names, names)
	if err != nil {
		return nil, err
	}
	return &resp, nil
}
